package leetcode.onehundred;

import java.util.Arrays;

//4. 寻找两个正序数组的中位数
public class FindMedianSortedArrays {
    public static void main(String[] args) {

        int[] nums1 = {1, 2};
        int[] nums2 = {3, 4};
        System.out.println(findMedianSortedArrays(nums1, nums2));
    }

    //合并数组
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        double res = 0;
        int index = 0;
        int[] p = new int[nums1.length + nums2.length];
        for (int i = 0; i < nums1.length; i++) {
            p[index++] = nums1[i];
        }
        for (int i = 0; i < nums2.length; i++) {
            p[index++] = nums2[i];
        }
        Arrays.sort(p);
//        System.out.println(Arrays.toString(p));
        int length = p.length;
        if (length % 2 == 0 && length != 0) {
            int x = p[length / 2];
            int y = p[(length / 2) - 1];
            res = (x + y) / 2.0;
        } else {
            res = p[length / 2];
        }
        return res;
    }
}
